//
//  DPPopView.m
//  DrinkingPlan
//
//  Created by 张德广 on 2019/3/15.
//  Copyright © 2019 Drinking Plan. All rights reserved.
//

#import "DPPopView.h"

@interface DPPopView ()

@property (nonatomic, strong) UIView *alertView;

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UIButton *dismissBtn;
@property (nonatomic, strong) UILabel *noteLabel;
@property (nonatomic, strong) UILabel *waterLabel;
@property (nonatomic, strong) UILabel *waterNum;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation DPPopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithWaterYieldStr:(NSString *)waterYieldStr {
    if (self = [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        
        self.alertView.frame = CGRectMake(0, 0, 280, 390);
        self.alertView.center = self.center;
        [self addSubview:self.alertView];
        
        self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, self.alertView.bounds.size.width-40, 100)];
        self.imgView.image = [UIImage imageNamed:@"popView"];
        self.imgView.contentMode = UIViewContentModeScaleAspectFit;
        [self.alertView addSubview:self.imgView];
        
        self.dismissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.dismissBtn.frame = CGRectMake(CGRectGetMaxX(self.alertView.bounds)-30-12, 10, 30, 30);
        [self.dismissBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
        [self.dismissBtn addTarget:self action:@selector(hiddenAction) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.dismissBtn];
        
        self.noteLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.imgView.frame)+20, self.alertView.bounds.size.width-40, 25)];
        self.noteLabel.text = @"IT'S TIME TO DRINK WATER.";
        self.noteLabel.textColor = UIColorOfHex(0xE94220);
        self.noteLabel.font = TextFont(14);
        self.noteLabel.textAlignment = NSTextAlignmentCenter;
        [self.alertView addSubview:self.noteLabel];
        
        self.waterLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.noteLabel.frame)+20, self.alertView.bounds.size.width-40, 20)];
        self.waterLabel.text = @"The amount of water this time";
        self.waterLabel.textColor = UIColorOfHex(0x666666);
        self.waterLabel.font = TextFont(11);
        self.waterLabel.textAlignment = NSTextAlignmentCenter;
        [self.alertView addSubview:self.waterLabel];
        
        self.waterNum = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.waterLabel.frame)+10, self.alertView.bounds.size.width-40, 30)];
        self.waterNum.text = [NSString stringWithFormat:@"%@ml",waterYieldStr];
        self.waterNum.textColor = UIColorOfHex(0x5ad7a1);
        self.waterNum.font = TextFont(25);
        self.waterNum.textAlignment = NSTextAlignmentCenter;
        [self.alertView addSubview:self.waterNum];
        
        self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.confirmBtn.frame = CGRectMake(CGRectGetMidX(self.alertView.bounds)-191/2, CGRectGetMaxY(self.waterNum.frame)+20, 191, 38);
        self.confirmBtn.backgroundColor = UIColorOfHex(0x5ad7a1);
        [self.confirmBtn setTitle:@"Complete" forState:UIControlStateNormal];
        [self.confirmBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        self.confirmBtn.titleLabel.font = TextFont(14);
        self.confirmBtn.layer.cornerRadius = 19;
        self.confirmBtn.tag = 100;
        [self.confirmBtn addTarget:self action:@selector(actionOfBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.confirmBtn];
        
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cancelBtn.frame = CGRectMake(CGRectGetMidX(self.alertView.bounds)-191/2, CGRectGetMaxY(self.confirmBtn.frame)+25, 191, 38);
        self.cancelBtn.backgroundColor = UIColorOfHexA(0x999999, 0.8);
        [self.cancelBtn setTitle:@"Not Complete" forState:UIControlStateNormal];
        [self.cancelBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        self.cancelBtn.titleLabel.font = TextFont(14);
        self.cancelBtn.layer.cornerRadius = 19;
        self.cancelBtn.tag = 200;
        [self.cancelBtn addTarget:self action:@selector(actionOfBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.cancelBtn];
    }
    return self;
}

- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc] init];
        _alertView.backgroundColor = ThemeColor;
        _alertView.layer.masksToBounds = YES;
        _alertView.layer.cornerRadius = 6;
        _alertView.userInteractionEnabled = YES;
    }
    return _alertView;
}

- (void)showView {
    self.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    
    self.alertView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.2, 0.2);
    self.alertView.alpha = 0;
    [UIView animateWithDuration:0.3f delay:0.1 usingSpringWithDamping:0.5f initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backgroundColor = UIColorOfHexA(0x333333, 0.5);
        self.alertView.transform = transform;
        self.alertView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismissAlertView {
    [UIView animateWithDuration:.2f animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(0.2, 0.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.08f
                         animations:^{
                             self.alertView.transform = CGAffineTransformMakeScale(0.25, 0.25);
                         }completion:^(BOOL finish){
                             [self removeFromSuperview];
                         }];
    }];
}

#pragma mark - actionEvents
- (void)hiddenAction {
    
    [self dismissAlertView];
}

- (void)actionOfBtn:(UIButton *)sender {
    
    [self dismissAlertView];
    if ([_delegate respondsToSelector:@selector(didSelectedBtn:withButtonTag:)]) {
        [_delegate didSelectedBtn:self withButtonTag:sender.tag];
    }
}




@end
